﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace App.Utility
{
    /// <summary>
    /// 验证码
    /// </summary>
    public static class DoValidCode
    {
        static string[] Nums = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
        static string[] LetLower = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
        static string[] LetUpper = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };

        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="length">验证码长度</param>
        /// <param name="isNumber">是否使用数字</param>
        /// <param name="isCantainLetter">是否使用小写字母</param>
        /// <param name="isCantainLetterUpper">是否使用大写字母</param>
        /// <returns></returns>
        public static string Create(int length, bool isNumber = true, bool isCantainLetter = false, bool isCantainLetterUpper = false)
        {
            if (length <= 0)
            {
                throw new Exception("验证码长度必须大于0");
            }

            List<string> tempObj = new List<string>();
            if (isNumber) { tempObj.AddRange(Nums.ToList()); }
            if (isCantainLetter) { tempObj.AddRange(LetLower.ToList()); }
            if (isCantainLetterUpper) { tempObj.AddRange(LetUpper.ToList()); }

            string resultStr = "";
            Random Rdm = new Random();
            var i = 0;
            while (i < length)
            {
                resultStr += tempObj[Rdm.Next(tempObj.Count - 1)];
                i++;
            }
            return resultStr;
        }

        /// <summary>
        /// 生成推荐码
        /// </summary>
        /// <param name="intLength">Length of the int.</param>
        /// <param name="booNumber">if set to <c>true</c> [boo number].</param>
        /// <param name="booSign">if set to <c>true</c> [boo sign].</param>
        /// <param name="booSmallword">if set to <c>true</c> [boo smallword].</param>
        /// <param name="booBigword">if set to <c>true</c> [boo bigword].</param>
        /// <returns></returns>
        public static string GetRandomizer(int intLength, bool booNumber, bool booSign, bool booSmallword, bool booBigword)
        {
            //定义
            Random ranA = new Random();
            int intResultRound = 0;
            int intA = 0;
            string strB = "";

            while (intResultRound < intLength)
            {
                //生成随机数A，表示生成类型
                //1=数字，2=符号，3=小写字母，4=大写字母

                intA = ranA.Next(1, 5);

                //如果随机数A=1，则运行生成数字
                //生成随机数A，范围在0-10
                //把随机数A，转成字符
                //生成完，位数+1，字符串累加，结束本次循环

                if (intA == 1 && booNumber)
                {
                    intA = ranA.Next(0, 10);
                    strB = intA.ToString() + strB;
                    intResultRound = intResultRound + 1;
                    continue;
                }

                //如果随机数A=2，则运行生成符号
                //生成随机数A，表示生成值域
                //1：33-47值域，2：58-64值域，3：91-96值域，4：123-126值域

                if (intA == 2 && booSign == true)
                {
                    intA = ranA.Next(1, 5);

                    //如果A=1
                    //生成随机数A，33-47的Ascii码
                    //把随机数A，转成字符
                    //生成完，位数+1，字符串累加，结束本次循环

                    if (intA == 1)
                    {
                        intA = ranA.Next(33, 48);
                        strB = ((char)intA).ToString() + strB;
                        intResultRound = intResultRound + 1;
                        continue;
                    }

                    //如果A=2
                    //生成随机数A，58-64的Ascii码
                    //把随机数A，转成字符
                    //生成完，位数+1，字符串累加，结束本次循环

                    if (intA == 2)
                    {
                        intA = ranA.Next(58, 65);
                        strB = ((char)intA).ToString() + strB;
                        intResultRound = intResultRound + 1;
                        continue;
                    }

                    //如果A=3
                    //生成随机数A，91-96的Ascii码
                    //把随机数A，转成字符
                    //生成完，位数+1，字符串累加，结束本次循环

                    if (intA == 3)
                    {
                        intA = ranA.Next(91, 97);
                        strB = ((char)intA).ToString() + strB;
                        intResultRound = intResultRound + 1;
                        continue;
                    }

                    //如果A=4
                    //生成随机数A，123-126的Ascii码
                    //把随机数A，转成字符
                    //生成完，位数+1，字符串累加，结束本次循环

                    if (intA == 4)
                    {
                        intA = ranA.Next(123, 127);
                        strB = ((char)intA).ToString() + strB;
                        intResultRound = intResultRound + 1;
                        continue;
                    }

                }

                //如果随机数A=3，则运行生成小写字母
                //生成随机数A，范围在97-122
                //把随机数A，转成字符
                //生成完，位数+1，字符串累加，结束本次循环

                if (intA == 3 && booSmallword == true)
                {
                    intA = ranA.Next(97, 123);
                    strB = ((char)intA).ToString() + strB;
                    intResultRound = intResultRound + 1;
                    continue;
                }

                //如果随机数A=4，则运行生成大写字母
                //生成随机数A，范围在65-90
                //把随机数A，转成字符
                //生成完，位数+1，字符串累加，结束本次循环

                if (intA == 4 && booBigword == true)
                {
                    intA = ranA.Next(65, 89);
                    strB = ((char)intA).ToString() + strB;
                    intResultRound = intResultRound + 1;
                    continue;
                }
            }
            return strB;

        }


    }
}
